/*
 * NewTableDialog.hpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   NewTableDialog.hpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   4 sep 2012
 */
#ifndef TABLESETTINGSDIALOG_HPP
#define TABLESETTINGSDIALOG_HPP

#include <wx/wx.h>
#include <wx/spinctrl.h>
#include <boost/shared_ptr.hpp>

#include "Resistance.hpp"
//#include "Voltage.hpp"
//#include "AdcValue.hpp"

#include "ThermistorVisitor.hpp"

using boost::shared_ptr;

namespace tltgen {

  class Thermistor;
  class AnalogToDigitalConverter;
  class Circuit;
  class CircuitPanel;
  class TemperatureScale;
  class ThermistorTable;
  class NewTableDialog : public wxDialog, public ThermistorVisitor {
  public:
    static const wxString INTEGER_CHARACTERS[];

    NewTableDialog(wxWindow* parent, wxWindowID id, const shared_ptr<TemperatureScale>& temperatureScale, const wxString& title = _("New Table"), 
		   const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxDEFAULT_DIALOG_STYLE);

    void setThermistorValues(const shared_ptr<Thermistor const>& thermistor);
    void setAdcValues(const shared_ptr<AnalogToDigitalConverter const>& adc);
    void setCircuitValues(const shared_ptr<Circuit const>& circuit);
    
    shared_ptr<ThermistorTable> createThermistorTable() const;
    shared_ptr<TemperatureScale> getTemperatureScale() const;

  protected:
    DECLARE_EVENT_TABLE();

    void initializeComponents();
    wxPanel* createTableSettingsPanel(wxWindow* parent);
    wxPanel* createTableSizePanel(wxWindow* parent);
    wxPanel* createTableSourcePanel(wxWindow* parent);
    wxPanel* createBetaPanel(wxWindow* parent);
    wxPanel* createShhPanel(wxWindow* parent);

    void onTableSourceChanged(wxCommandEvent &event);
    void updateTableSourcePanelsEnabled();

    wxString formatTemperature(Temperature t) const;
    wxString formatResistance(Resistance r) const;
    wxString formatBetaValue(double b) const;
    wxString formatShhParameter(double v) const;

    double getValueAsFloat(const wxString& string) const;
    //int getValueAsInt(const wxString& string) const;
 
    void setBetaValues(const BetaModelThermistor* thermistor) const;
    void setShhValues(const NormalizedSteinhartHartModelThermistor* thermistor) const;

    bool isEmptyTableSelected() const;
    bool isBetaModelTableSelected() const;
    bool isShhModelTableSelected() const;

    Resistance getBetaModelT0() const;
    Resistance getBetaModelR0() const;
    double getBetaModelBeta() const;
    void setBetaPanelEnabled(bool value);

    Resistance getShhModelR0() const;
    double getShhModelA() const;
    double getShhModelB() const;
    double getShhModelC() const;
    double getShhModelD() const;
    void setShhPanelEnabled(bool value);

    int getNbTableRows() const;
    Temperature getLowestTemperature() const;
    Temperature getHighestTemperature() const;
    void setTableSizePanelEnabled(bool value);

    shared_ptr<Thermistor> createThermistor() const;
    void fillThermistorTable(const shared_ptr<ThermistorTable>& thermistorTable, const shared_ptr<Thermistor>& thermistor) const;

    virtual void visit(const BetaModelThermistor* thermistor);
    virtual void visit(const NormalizedSteinhartHartModelThermistor* thermistor);
    virtual void visit(const LinearInterpolationThermistor* thermistor);

    wxRadioButton *radEmptyTable;
    wxRadioButton *radBetaGen;
    wxRadioButton *radShhGen;

    wxTextCtrl *txtBetaT0;
    wxTextCtrl *txtBetaR0;
    wxTextCtrl *txtBetaBeta;
    wxTextCtrl *txtShhR0;
    wxTextCtrl *txtShhA;
    wxTextCtrl *txtShhB;
    wxTextCtrl *txtShhC;
    wxTextCtrl *txtShhD;

    wxSpinCtrl* spinNbRows;
    wxTextCtrl* txtFromTemp;
    wxTextCtrl* txtToTemp;

    CircuitPanel* circuitPanel;

    shared_ptr<TemperatureScale> temperatureScale;
  };

  enum {
    ID_RadEmpty = 1,
    ID_RadBeta,
    ID_RadShh,
  };

}

#endif
